package com;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;

public class C {
    public static void main(String[] args) throws InterruptedException {
        long starttime =System.currentTimeMillis();
        Map<String, Integer> map1 = new HashMap<>();
        List<Map<String, Integer>> maps = new ArrayList<Map<String, Integer>>();
        for (int i = 0; i <180 ; i++) {
            Map<String, Integer> map = new HashMap<>();
            map.put("name", i);
            map.put("age", i*10+1);
            maps.add(map);
        }
        CountDownLatch c1=new CountDownLatch(100*180);
        for (int k = 0; k<100 ; k++) {
            for (final  Map<String, Integer> map : maps) {
                if(map.get("name")*10+1!= map.get("age")){
                    System.out.println(map.get("name")+"====="+map.get("age"));
                }
            }
           // System.out.println("==============");
            for (final  Map<String, Integer> map : maps) {
                Thread t = new Thread(new MyThread1(map,map1,c1));
//                Thread t = new Thread(new MyThread1(map));
                // 启动线程
                //t.run(); // 不会启动线程，不会分配新的分支栈。（这种方式就是单线程。）
                t.start();
                // 这里的代码还是运行在主线程中。
            }
        }
        c1.await();
        long endtime =System.currentTimeMillis();
        System.out.println(endtime-starttime);
    }
    static class MyThread1 implements Runnable {
        public static Object lock = new Object();
        private  Map<String, Integer> map;
//        private  Map<String, Integer> map1 =new HashMap<>();
        private  Map<String, Integer> map1 ;
        private   CountDownLatch c1 ;
        public MyThread1(Map<String, Integer> map, Map<String, Integer> map1, CountDownLatch c1) {
            this.map=map;
            this.map1=map1;
            this.c1=c1;
        }

        public MyThread1(Map<String, Integer> map) {
            this.map=map;
        }



        @Override
        public void run() {
//            System.out.println(Thread.currentThread().getId());
            if(map.get("name")*10+1!= map.get("age")){
                System.out.println(map.get("name")+"====="+map.get("age"));
            }
//            System.out.println(map.get("name")+"====="+map.get("age"));
            synchronized (map1){
            map1.put("name",map.get("name"));
            map1.put("age",map.get("age"));

            if(map1.get("name")*10+1!= map1.get("age")){
                System.out.println(map.get("name")+"====="+map.get("age"));
                System.out.println(map1.get("name")+"====="+map1.get("age"));
                System.out.println("*****************");
            }
            if(map1.get("name")!= map.get("name")){
                System.out.println(map.get("name")+"====="+map.get("age"));
                System.out.println(map1.get("name")+"====="+map1.get("age"));
                System.out.println("%%%%%%%%%%%%%%%%%%%%%%");
            }
            c1.countDown();
        }
////            System.out.println(map.get("name")+"=============="+map1.get("name"));
//            if(!map.get("name").equals(map1.get("name"))){
//                System.out.println(map.get("name")+"=============="+map1.get("name"));
//            }
        }
    }

}
